package com.ruoyi.web.exams.controller;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.exams.domain.ExamResult;
import com.ruoyi.web.exams.dto.ExamUserResult;
import com.ruoyi.web.exams.service.IExamResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 试卷答题结果概览Controller
 *
 * @author 范书铭
 * @date 2024-10-02
 */
@Api("试卷答题结果概览Controller")

@RestController
@RequestMapping("/exam/result")
public class ExamResultController extends BaseController {
    @Autowired
    private IExamResultService examResultService;

    /**
     * 查询试卷答题结果概览列表
     */
    @ApiOperation("查询试卷答题结果概览列表")
    @PreAuthorize("@ss.hasPermi('exam:result:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamResult examResult) {
        startPage();
        List<ExamResult> list = examResultService.selectExamResultList(examResult);
        return getDataTable(list);
    }

    /**
     * 导出试卷答题结果概览列表
     */
    @ApiOperation("导出试卷答题结果概览列表")
    @PreAuthorize("@ss.hasPermi('exam:result:export')")
    @Log(title = "试卷答题结果概览", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamResult examResult) {
        List<ExamResult> list = examResultService.selectExamResultList(examResult);
        ExcelUtil<ExamResult> util = new ExcelUtil<ExamResult>(ExamResult.class);
        util.exportExcel(response, list, "试卷答题结果概览数据");
    }

    /**
     * 获取试卷答题结果概览详细信息
     */
    @ApiOperation("获取试卷答题结果概览详细信息")
    @PreAuthorize("@ss.hasPermi('exam:result:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(examResultService.selectExamResultById(id));
    }

    /**
     * 新增试卷答题结果概览
     */
    @ApiOperation("新增试卷答题结果概览")
    @PreAuthorize("@ss.hasPermi('exam:result:add')")
    @Log(title = "试卷答题结果概览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamResult examResult) {
        return toAjax(examResultService.insertExamResult(examResult));
    }

    /**
     * 修改试卷答题结果概览
     */
    @ApiOperation("修改试卷答题结果概览")
    @PreAuthorize("@ss.hasPermi('exam:result:edit')")
    @Log(title = "试卷答题结果概览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamResult examResult) {
        return toAjax(examResultService.updateExamResult(examResult));
    }

    /**
     * 删除试卷答题结果概览
     */
    @ApiOperation("删除试卷答题结果概览")
    @PreAuthorize("@ss.hasPermi('exam:result:remove')")
    @Log(title = "试卷答题结果概览", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examResultService.deleteExamResultByIds(ids));
    }

    /**
     * 交卷
     */
    @ApiOperation("交卷")
    @PreAuthorize("@ss.hasPermi('exam:result:edit')")
    @GetMapping(value = "/{resultId}")
    public AjaxResult postPager(@PathVariable("resultId") Long resultId) {
        return success(examResultService.postPager(resultId));
    }

    /**
     * 用户查询试卷答题结果概览列表
     */
    @ApiOperation("用户查询试卷答题结果概览列表")
    @PreAuthorize("@ss.hasPermi('exam:result:list')")
    @GetMapping("/getUserResults")
    @DataScope(userAlias = "u")
    public TableDataInfo getUserResults() {
        startPage();
        List<ExamUserResult> list = examResultService.getUserResults();
        return getDataTable(list);
    }
}
